package com.sirun.cn.fca.presenter.impl;

import com.sirun.cn.fca.loader.HomeLoader;
import com.sirun.cn.fca.presenter.IHomePresenter;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.rx.SimpleObserver;

import cn.sirun.com.fca.account.model.base.CarInfoDetailResponse;
import cn.sirun.com.fca.account.model.login.PersonalInfo;
import cn.sirun.com.fca.account.model.login.PersonalInfoRequest;
import cn.sirun.com.fca.account.presenter.AccountFlowPresenter;
import cn.sirun.com.fca.personal.center.constant.PersonalCenterActionConst;
import cn.sirun.com.fca.personal.center.model.bean.CarListResponse;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.subjects.BehaviorSubject;

public class HomePresenter extends AccountFlowPresenter<HomeLoader> implements IHomePresenter {

    private BehaviorSubject<BaseResponse> mBehaviorSubject = BehaviorSubject.create();

    @Override
    protected HomeLoader createLoader() {
        return new HomeLoader();
    }

    @Override
    public void getPersonalInfo(PersonalInfoRequest request) {
        getLoader().getPersonalInfo(request)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<PersonalInfo>>() {

                    @Override
                    public void onNext(BaseResponse<PersonalInfo> personalInfoResponse) {
                        super.onNext(personalInfoResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_GET_PERSONAL_INFO_RESPONSE,
                                        personalInfoResponse);
                    }
                });
    }

    @Override
    public void getCarList() {
        getLoader().getCarList()
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<CarListResponse>>() {

                    @Override
                    public void onNext(BaseResponse<CarListResponse> carResponse) {
                        super.onNext(carResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_GET_CAR_LIST_RESPONSE,
                                        carResponse);
                    }
                });
    }

    @Override
    public void getCarInfo(String din) {
        getLoader().getCarInfo(din)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<CarInfoDetailResponse>>() {

                    @Override
                    public void onNext(BaseResponse<CarInfoDetailResponse> carInfoResponse) {
                        super.onNext(carInfoResponse);
                        getActionListener().onAction
                                (PersonalCenterActionConst.ACTION_GET_CAR_INFO_RESPONSE,
                                        carInfoResponse);
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBehaviorSubject.onComplete();
    }
}
